//
//  UIView+TXKit.m
//  TXKit
//
//  Created by YM on 2017/5/16.
//  Copyright © 2017年 Tincent. All rights reserved.
//

#import "UIView+TXKit.h"
#import <Masonry.h>
#import "TXKitMacro.h"
#import "UIImage+TXKit.h"

@implementation UIView (TXKit)

+ (UILabel *)labelText:(NSString *)text
                  font:(UIFont *)font
                 color:(UIColor *)color
                 align:(NSTextAlignment )align
                 lines:(NSInteger )lines
{
    UILabel *label = [[UILabel alloc] init];
    [label setFont:font];
    [label setTextColor:color];
    [label setText:text];
    [label setTextAlignment:align];
    [label setNumberOfLines:lines];
    
    return label;
}


+ (UIButton *)buttonWithTitle:(NSString *)title
                         font:(UIFont *)font
                        color:(UIColor *)color
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
    [button setTitleColor:color forState:UIControlStateNormal];
    
    return button;
}

+ (UIButton * )buttonWithTitle:(NSString *)title
                          font:(UIFont *)font
                         color:(UIColor *)color
                         image:(UIImage *)image
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    
    return button;
}

+ (UIButton *)buttonWithTitle:(NSString *)title
                         font:(UIFont *)font
                        color:(UIColor *)color
                      hlColor:(UIColor *)hlColor
                    seltColor:(UIColor *)seltColor
                     disColor:(UIColor *)disColor
                      bgImage:(UIImage *)bgImage
                    hlBgImage:(UIImage *)hlBgImage
                    seltImage:(UIImage *)seltImage
                     disImage:(UIImage *)disImage
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
    
    [button setTitleColor:color forState:UIControlStateNormal];
    if (hlColor) {
        [button setTitleColor:hlColor forState:UIControlStateHighlighted];
    }
    if (seltColor) {
        [button setTitleColor:seltColor forState:UIControlStateSelected];
    }
    if (disColor) {
        [button setTitleColor:disColor forState:UIControlStateDisabled];
    }
    
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    if (hlBgImage) {
        [button setBackgroundImage:hlBgImage forState:UIControlStateHighlighted];
    } else {
        [button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    }
    if (seltImage) {
        [button setBackgroundImage:seltImage forState:UIControlStateSelected];
    }
    if (disImage) {
        [button setBackgroundImage:disImage forState:UIControlStateDisabled];
    }
    
    return button;
}

+ (UIButton *)butonWithTitle:(NSString *)title
                        font:(UIFont *)font
                       color:(UIColor *)color
                     hlColor:(UIColor *)hlColor
                   seltColor:(UIColor *)seltColor
                     bgColor:(UIColor *)bgColor
                   hlBgColor:(UIColor *)hlBgColor
                 seltBgColor:(UIColor *)seltBgColor
{
    UIImage *bgImage = [UIImage imageWithColor:bgColor];
    UIImage *hlBgImage = hlBgColor ? [UIImage imageWithColor:hlBgColor] : nil;
    UIImage *seltBgImage = seltBgColor ? [UIImage imageWithColor:seltBgColor] : nil;
    
    return [self buttonWithTitle:title font:font color:color hlColor:hlColor seltColor:seltColor disColor:nil bgImage:bgImage hlBgImage:hlBgImage seltImage:seltBgImage disImage:nil];
}

+ (UIButton *)butonWithTitle:(NSString *)title
                        font:(UIFont *)font
                       color:(UIColor *)color
                     hlColor:(UIColor *)hlColor
                    disColor:(UIColor *)disColor
                     bgColor:(UIColor *)bgColor
                   hlBgColor:(UIColor *)hlBgColor
                  disBgColor:(UIColor *)disBgColor
{
    UIImage *bgImage = [UIImage imageWithColor:bgColor];
    UIImage *hlBgImage = hlBgColor ? [UIImage imageWithColor:hlBgColor] : nil;
    UIImage *disBgImage = disBgColor ? [UIImage imageWithColor:disBgColor] : nil;
    
    return [self buttonWithTitle:title font:font color:color hlColor:hlColor seltColor:nil disColor:disColor bgImage:bgImage hlBgImage:hlBgImage seltImage:nil disImage:disBgImage];
}

+ (UIButton *)butonWithTitle:(NSString *)title
                        font:(UIFont *)font
                       color:(UIColor *)color
                     bgColor:(UIColor *)bgColor
                   hlBgColor:(UIColor *)hlBgColor
                  disBgColor:(UIColor *)disBgColor
{
    UIImage *bgImage = [UIImage imageWithColor:bgColor];
    UIImage *hlBgImage = hlBgColor ? [UIImage imageWithColor:hlBgColor] : nil;
    UIImage *disBgImage = disBgColor ? [UIImage imageWithColor:disBgColor] : nil;
    
    return [self buttonWithTitle:title font:font color:color hlColor:nil seltColor:nil disColor:nil bgImage:bgImage hlBgImage:hlBgImage seltImage:nil disImage:disBgImage];
}

+ (UIButton *)butonWithTitle:(NSString *)title
                        font:(UIFont *)font
                       color:(UIColor *)color
                     bgColor:(UIColor *)bgColor
                   hlBgColor:(UIColor *)hlBgColor
{
    UIImage *bgImage = [UIImage imageWithColor:bgColor];
    UIImage *hlBgImage = hlBgColor ? [UIImage imageWithColor:hlBgColor] : nil;
    
    return [self buttonWithTitle:title font:font color:color hlColor:nil seltColor:nil disColor:nil bgImage:bgImage hlBgImage:hlBgImage seltImage:nil disImage:nil];
}


+ (UIImageView *)imageViewWithImage:(UIImage *)image
                               mode:(UIViewContentMode )contentMode
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.clipsToBounds = YES;
    [imageView setContentMode:contentMode];
    
    return imageView;
}


+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder
                                     font:(UIFont *)font
                                    color:(UIColor *)color
                                 delegate:(id<UITextFieldDelegate>)delegate
{
    UITextField *textField = [[UITextField alloc] init];
    textField.delegate = delegate;
    textField.backgroundColor = [UIColor clearColor];
    textField.font = font;
    textField.textColor = color;
    textField.textAlignment = NSTextAlignmentLeft;
    textField.borderStyle = UITextBorderStyleNone;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.secureTextEntry = NO;
    textField.autocorrectionType = UITextAutocorrectionTypeDefault;
    textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    textField.enablesReturnKeyAutomatically = YES;
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
    textField.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:TX_COLOR_FROM_RGB(0x808080)}];
    ///
//    textField.placeholder = placeholder;
//    [textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    return textField;
}


+ (UIScrollView *)scrollViewWithPaging:(BOOL )pagingEnabled
                              delegate:(id<UIScrollViewDelegate>)delegate
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [scrollView setDelegate:delegate];
    [scrollView setPagingEnabled:pagingEnabled];
    [scrollView setClipsToBounds:YES];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    
    return scrollView;
}


+ (UITableView *)tableViewWithDataSource:(id<UITableViewDataSource>)dataSource
                                delegate:(id<UITableViewDelegate>)delegate
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableView setDelegate:delegate];
    [tableView setDataSource:dataSource];
    
    return tableView;
}


+ (UIView *)aView
{
    UIView *view = [UIView new];
    
    return view;
}

+ (UIView *)splitLine
{
    UIView *view = [UIView new];
    view.backgroundColor = TX_COLOR_FROM_RGB(0xF2F2F2);
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    return view;
}


- (void)setLayerCorner:(CGFloat)radius
{
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}

- (void)setLayerCorner:(CGFloat)radius
           borderWidth:(CGFloat )width
           borderColor:(UIColor *)color
{
    self.layer.cornerRadius = radius;
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
    self.clipsToBounds = YES;
}

- (void)removeBorders
{
    self.layer.borderWidth = 0;
    self.layer.cornerRadius = 0;
    self.layer.borderColor = nil;
}

- (BOOL)isShowingOnKeyWindow
{
    //主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  
    //以窗口的左上角为坐标原点，计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect windowBounds = keyWindow.bounds;
    //判断newFrame是不是在windowBounds上
    BOOL isOnWindow = CGRectIntersectsRect(newFrame, windowBounds);
 
    return self.window == keyWindow && !self.hidden && self.alpha > 0.01 && isOnWindow;
}

@end
